/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.controller;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.common.documentoperate.dto.StageDocCommonOperateSaveDto;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.DocumentCommonFormDataVo;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.DocumentCommonFormHtmlVo;
import com.icinfo.cloud.provider.punish.dto.*;
import com.icinfo.cloud.provider.punish.service.IAppCaseBeforeDocumentRecordService;
import com.icinfo.cloud.provider.punish.vo.*;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import com.icinfo.framework.tools.utils.MapUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 描述: 移动端办案直通车-TRJ app_case_before_document_record 对应的Controller类.<br>
 *
 * @author framework generator
 * @date 2022年04月12日
 */
@RestController
@RequestMapping("/app/document")
public class AppCaseBeforeDocumentRecordController extends BaseController {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(AppCaseBeforeDocumentRecordController.class);

    @Autowired
    private IAppCaseBeforeDocumentRecordService appCaseBeforeDocumentRecordService;

    /**
     * 【V1.0.0】描述：移动端办案直通车-保存文书信息通用接口
     *
     * @param saveDto {@link StageDocCommonOperateSaveDto}
     * @return {@link Result<String>}
     * @author cyy
     */
    @PostMapping("/saveDocInfo")
    @ApiOperation(value = "【V1.0.0】描述：保存文书信息通用接口", httpMethod = "POST")
    public Result saveDocInfo(@RequestBody @Validated AppDocumentCommonSaveDto saveDto) {
        if (Constant.STRING_2.equals(saveDto.getOperateType())) {
            AppDocumentCommonModifyVo resultMap = appCaseBeforeDocumentRecordService.modifyDocumentInfo(saveDto);
            String t = "true";
            if (ObjectUtil.isNotEmpty(resultMap) && t.equals(resultMap.getSuccess())) {
                return Result.success(Constant.STRING_SAVE_SUCCESS, resultMap);
            }
        } else {
            Map<String, Object> resultMap = appCaseBeforeDocumentRecordService.saveDocInfo(saveDto);
            String status = MapUtils.getString(resultMap, "success", "");
            String t = "true";
            if (t.equals(status)) {
                return Result.success(Constant.STRING_SAVE_SUCCESS, resultMap);
            }
        }
        return Result.error(Constant.STRING_SAVE_FAIL);

    }

    /**
     * 【V1.0.0】描述：移动端办案直通车-常用文书列表
     *
     * @param {@link }
     * @return {@link Result<String>}
     * @author cyy
     */
    @PostMapping("/getCommonlyUseDocumentList")
    @ApiOperation(value = "【V1.0.0】描述：移动端办案直通车-常用文书列表", httpMethod = "POST")
    public Result<List<AppDocumentListSimpleVo>> getCommonlyUseDocumentList() {
        /**
         * 时间范围：当前账号近7天使用过的；
         * 暂不限制数量，没有的话整块隐藏
         */
        return Result.success(appCaseBeforeDocumentRecordService.getCommonlyUseDocumentList());
    }

    /**
     * 【V1.0.0】描述：移动端办案直通车-根据文书目录编码查询关联已生成文书
     *
     * @param {@link }
     * @return {@link Result<String>}
     * @author cyy
     */
    @PostMapping("/getRelationDocumentListByCatalogCode")
    @ApiOperation(value = "【V1.0.0】描述：根据文书目录编码查询关联已生成文书", httpMethod = "POST")
    public Result<List<AppRelationDocumentListVo>> getRelationDocumentListByCatalogCode(@RequestBody @Validated DocCommonHtmlRequest request) {
        /**
         * 查询配置表 关联文书编码，查询已生成文书记录
         */
        return Result.success(appCaseBeforeDocumentRecordService.getRelationDocumentListByCatalogCode(request));
    }

    /**
     * 【V1.0.0】描述：移动端办案直通车-文书id查询关联文书详情
     *
     * @param {@link }
     * @return {@link Result<String>}
     * @author cyy
     */
    @PostMapping("/getRelationDocumentDetailInfo")
    @ApiOperation(value = "【V1.0.0】描述：文书id查询关联文书详情", httpMethod = "POST")
    public Result<AppRelationDocumentListVo> getRelationDocumentDetailInfo(@RequestBody @Validated DocumentDetailQueryDto request) {
        /**
         * 查询已生成文书详情
         */
        return Result.success(appCaseBeforeDocumentRecordService.getRelationDocumentDetailInfo(request));
    }

    /**
     * 【V1.0.0】描述：移动端办案直通车-全部文书列表
     *
     * @param {@link }
     * @return {@link Result<String>} 保存结果
     * @author cyy
     */
    @PostMapping("/getAllDocumentList")
    @ApiOperation(value = "【V1.0.0】描述：移动端办案直通车-全部文书列表", httpMethod = "POST")
    public Result<List<AppDocumentListSimpleVo>> getAllDocumentList() {
        return Result.success(appCaseBeforeDocumentRecordService.getAllDocumentList());
    }

    /**
     * 【V1.0.0】描述：移动端办案直通车-历史文书列表
     *
     * @param dto
     * @return {@link Result<String>}
     * @author cyy
     */
    @PostMapping("/getHisDocumentList")
    @ApiOperation(value = "【V1.0.0】描述：移动端办案直通车-历史文书列表", httpMethod = "POST")
    public Result<PageResponse<AppHisDocumentListVo>> getHisDocumentList(@RequestBody @Validated AppHisDocQueryDto dto) {
        return Result.success("success", new PageResponse<>(appCaseBeforeDocumentRecordService.getHisDocumentList(dto)));
    }

    /**
     * 获取文书html公共方法
     *
     * @param request
     * @return {@link Result< String>}
     * @author cyy
     */
    @PostMapping("/getDocBaseInfo")
    public Result<DocumentCommonFormHtmlVo> getDocHtml(@RequestBody @Validated DocCommonHtmlRequest request) {
        DocumentCommonFormHtmlVo result = appCaseBeforeDocumentRecordService.getDocumentFormHtml(request);
        return Result.success(Constant.STRING_GET_SUCCESS, result);
    }

    /**
     * 获取文书数据公共方法
     *
     * @param dto
     * @return {@link Result<String> }
     * @author cyy
     */
    @PostMapping("/getDocFormData")
    public Result<DocumentCommonFormDataVo> getDocFormData(@RequestBody @Validated DocCommonHtmlRequest dto) {
        DocumentCommonFormDataVo result = appCaseBeforeDocumentRecordService.getCommonDocumentFormData(dto);
        return Result.success(Constant.STRING_GET_SUCCESS, result);
    }

    /**
     * 查询企业法人信息列表
     *
     * @param dto
     * @return {@link Result<String> }
     * @author cyy
     */
    @PostMapping("/getEntList")
    public Result getEntList(@RequestBody @Validated EntQueryDto dto) {
        if (StringUtils.isEmpty(dto.getFrmc()) && StringUtils.isEmpty(dto.getTyxydm())) {
            return Result.error("请输入查询数据");
        }
        return Result.success(Constant.STRING_GET_SUCCESS, appCaseBeforeDocumentRecordService.getEntList(dto));
        // return appCaseBeforeDocumentRecordService.getEntList(dto);
    }

    /**
     * 查询企业法人信息列表
     *
     * @param dto
     * @return {@link Result<String> }
     * @author cyy
     */
    @PostMapping("/getCqEntList")
    public Result getCqEntList(@RequestBody @Validated EntQueryDto dto) {
        if (StringUtils.isEmpty(dto.getFrmc()) && StringUtils.isEmpty(dto.getTyxydm())) {
            return Result.error("请输入查询数据");
        }
        //return Result.success(Constant.STRING_GET_SUCCESS, appCaseBeforeDocumentRecordService.getEntList(dto));
        return appCaseBeforeDocumentRecordService.getCqEntList(dto);
    }


    /**
     * 【V1.0.0】描述：移动端办案直通车-接收文书签字捺印数据
     *
     * @param saveDto {@link StageDocCommonOperateSaveDto}
     * @return {@link Result<String>}
     * @author cyy
     */
    @PostMapping("/receiveSignFile")
    @ApiOperation(value = "【V1.0.0】描述：移动端办案直通车-接收文书签字捺印数据", httpMethod = "POST")
    public Result receiveSignFile(@RequestBody @Validated AppDocumentSignSaveDto saveDto) {
        Map<String, Object> resultMap = appCaseBeforeDocumentRecordService.receiveSignFile(saveDto);
        String status = MapUtils.getString(resultMap, "success", "");
        String t = "true";
        if (t.equals(status)) {
            return Result.success(Constant.STRING_SAVE_SUCCESS, resultMap);
        }
        return Result.error(Constant.STRING_SAVE_FAIL);
    }


    /**
     * 移动端办案直通车-接受重庆的文件id来做操作
     */
    @PostMapping("/receiveSignFileId")
    public Result receiveSignFileId(@RequestBody @Validated AppDocumentSignDto saveDto) {
        Map<String, Object> resultMap = appCaseBeforeDocumentRecordService.receiveSignFileId(saveDto);
        String status = MapUtils.getString(resultMap, "success", "");
        String t = "true";
        if (t.equals(status)) {
            return Result.success(Constant.STRING_SAVE_SUCCESS, resultMap);
        }
        return Result.error(Constant.STRING_SAVE_FAIL);
    }

    /**
     * 获取ocr文字, app 会使用
     *
     * @param file
     * @return
     */
    @PostMapping("/getOcrData")
    public Result getOcrData(@RequestParam("file") MultipartFile file) {
        return appCaseBeforeDocumentRecordService.getOcrData(file);
    }


    /**
     * 重庆接口token app 会使用
     *
     * @return
     */
    @GetMapping("/getToken")
    public Result<String> getToken() {
        return Result.success("获取成功", appCaseBeforeDocumentRecordService.getToken());
    }


    /**
     * 【V1.4.1】描述： 获取上一次询问通知书地址
     *
     * @return {@link Result< String>}
     * @author: zhengqiang
     * @date: 2022/8/31
     **/
    @GetMapping("/getHisNoticeOfEnquiryAddress")
    public Result<String> getHisNoticeOfEnquiryAddress() {
        String hisNoticeOfEnquiryAddress = appCaseBeforeDocumentRecordService.getHisNoticeOfEnquiryAddress();
        return Result.success(Constant.STRING_SAVE_SUCCESS, hisNoticeOfEnquiryAddress);
    }


//    /**
//     * 【V1.11.0】描述：修改办案直通车文书信息通用接口
//     *
//     * @param saveDto 保存dto
//     * @return {@link Result }
//     * @author shishengyao
//     * @date 2023/02/20
//     */
//    @PostMapping("/modifyDocumentInfo")
//    @ApiOperation(value = "【V1.0.0】描述：保存文书信息通用接口", httpMethod = "POST")
//    public Result modifyDocumentInfo(@RequestBody @Validated AppDocumentCommonModifyDto saveDto) {
//        AppDocumentCommonModifyVo resultMap = appCaseBeforeDocumentRecordService.modifyDocumentInfo(saveDto);
//        String t = "true";
//        if (ObjectUtil.isNotEmpty(resultMap) && t.equals(resultMap.getSuccess())) {
//            return Result.success(Constant.STRING_SAVE_SUCCESS,resultMap);
//        }
//        return Result.error(Constant.STRING_SAVE_FAIL);
//    }

    /**
     * 【V1.11.0】描述：获取办案直通车文书详情通用接口
     *
     * @param documentId 文书id
     * @return {@link Result< AppCaseBeforeDocumentDetailVo > }
     * @author shishengyao
     * @date 2023/02/21
     */
    @GetMapping("/getAppDocumentDetail")
    @ApiOperation(value = "【V1.0.0】描述：获取办案直通车文书详情通用接口", httpMethod = "get")
    public Result<AppCaseBeforeDocumentDetailVo> getAppDocumentDetail(@RequestParam String documentId) {
        AppCaseBeforeDocumentDetailVo vo = appCaseBeforeDocumentRecordService.getAppDocumentDetailByDocumentId(documentId);
        if (ObjectUtil.isNotEmpty(vo)) {
            return Result.success(Constant.STRING_SAVE_SUCCESS, vo);
        }
        return Result.error(Constant.STRING_SAVE_FAIL);
    }

    /**
     * 【V1.3.2】描述：移动端办案直通车-接收文书签字捺印数据
     *
     * @param dto {@link StageDocCommonOperateSaveDto}
     * @return {@link Result<String>}
     * @author cyy
     */
    @PostMapping("/sendSmsNotify")
    @ApiOperation(value = "【V1.0.0】描述：移动端办案直通车-发送短信通知", httpMethod = "POST")
    public Result<String> sendSmsNotify(@RequestBody @Validated AppDocumentSendNotifyDto dto) {
        return appCaseBeforeDocumentRecordService.sendSmsNotify(dto);
    }


    /**
     * 获取我的案源/案件/检查任务列表
     */
    @GetMapping("/getAssociatedTaskList")
    @ApiOperation(value = "获取我的案源/案件/检查任务列表", httpMethod = "GET")
    public Result<PageResponse<AssociatedTaskVo>> getAssociatedTaskList(@Validated MyAssociatedTaskDto dto) {
        return Result.success(Constant.INTEGER_200, appCaseBeforeDocumentRecordService.getAssociatedTaskList(dto));
    }

    /**
     * 获取当事人信息
     */
    @GetMapping("/getAssociatedTaskParties")
    @ApiOperation(value = "获取案源/案件/检查当事人", httpMethod = "GET")
    public Result<List<AssociatedTaskPartiesVo>> getAssociatedTaskParties(@Validated AssociatedTaskPartiesDto dto) {
        return Result.success(Constant.INTEGER_200, appCaseBeforeDocumentRecordService.getAssociatedTaskParties(dto));
    }

    /**
     * 直通车文书：提交时不签字
     */
    @PostMapping("/submitWithoutSignature")
    @ApiOperation(value = "直通车文书：不签字提交", httpMethod = "POST")
    public Result<String> submitWithoutSignature(@RequestBody @Validated SubmitWithoutSignatureDto dto) {
        return Result.success(appCaseBeforeDocumentRecordService.submitWithoutSignature(dto));
    }

    /**
     * 直通车文书：重新签字
     */
    @PostMapping("/signAgain")
    @ApiOperation(value = "直通车文书：重新签字", httpMethod = "POST")
    public Result<String> signAgain(@RequestBody @Validated SignAgainDto dto) {
        return Result.success(appCaseBeforeDocumentRecordService.signAgain(dto));
    }


    /**
     * 直通车文书：删除文书
     */
    @PostMapping("/deleteDocument")
    @ApiOperation(value = "直通车文书：删除文书", httpMethod = "POST")
    public Result<String> deleteDocument(@RequestBody @Validated DeleteDocumentDto dto) {
        return appCaseBeforeDocumentRecordService.deleteDocument(dto);
    }

    /**
     * 直通车文书：校验文书是否有关联任务且任务是否处于可以操作的状态
     */
    @GetMapping("/verifyAssociatedTask")
    @ApiOperation(value = "直通车文书：校验文书是否有关联任务且任务是否处于可以操作的状态", httpMethod = "POST")
    public Result<VerifyAssociatedTaskVo> verifyAssociatedTask(@Validated VerifyAssociatedTaskDto dto) {
        return Result.success(Constant.INTEGER_200, appCaseBeforeDocumentRecordService.verifyAssociatedTask(dto));
    }


    /**
     * 描述：【v1.3.4】分页获取获取办案直通车文书列表
     *
     * @param dto
     * @return {@link Result <AppCaseHisDocumentListVo> }
     * @author lujiayu
     * @date 2024/01/10
     */
    @GetMapping("/page")
    @ApiOperation(value = "描述：分页获取获取办案直通车文书列表", httpMethod = "GET")
    public Result<PageResponse<AppCaseDocumentVo>> page(@Validated AppDocQueryDto dto) {
        return Result.success("查询成功", appCaseBeforeDocumentRecordService.page(dto));
    }

    @GetMapping("/getDocumentUrlByDocumentId")
    @ApiOperation(value = "通过文书id获取文书地址")
    public Result<String> getDocumentUrlByDocumentId(@RequestParam String documentId) {
        return Result.success("查询成功", appCaseBeforeDocumentRecordService.getDocumentUrlByDocumentId(documentId));
    }

}
